import Vue from 'vue'
import VueRouter from 'vue-router'

/* Layout */
import Layout from '@/layout'

Vue.use(VueRouter)

//获取原型对象上的push函数
const originalPush = VueRouter.prototype.push
//修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

const routes = [
    {
        path: '/',
        component: Layout, // 修改为 Layout 组件
        children: [
            {
                path: '',
                component: () => import( '../views/HomeView.vue'),
                name: 'home'
            }
        ]
    },
    {
        path: '/about',
        component: Layout, // 修改为 Layout 组件
        children: [
            {
                path: '',
                component: () => import( '../views/AboutView.vue'),
                name: 'about'
            }
        ]
    },
    {
        path: '/login',
        name: 'login',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "about" */ '../views/LoginView.vue')
    },
    {
        path: '/register',
        name: 'register',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "about" */ '../views/RegisterView.vue')
    },
    {
        path: '/rbac',
        component: Layout,
        children: [
            {
                path: 'user/list',
                component: () => import(/* webpackChunkName: "about" */ '../views/User/ListView.vue')
            },
            {
                path: 'user/add',
                component: () => import(/* webpackChunkName: "about" */ '../views/User/AddView.vue')
            },
            {
                path: 'user/password',
                component: () => import(/* webpackChunkName: "about" */ '../views/User/ChangePasswordView.vue')
            }
        ]
    },
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

export default router
